﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MossDAL.DotNet
{
    public static class CamlFactory
    {
        public enum OrderByType
        {
            Ascending,
            Decending
        }

        public static string CreateConditionalClause(string conditionType, string condition)
        {
            return "<" + conditionType + ">" + condition + "</" + conditionType + ">";
        }

        public static string CreateAndClause(string condition)
        {
            return CreateConditionalClause("And", condition);
        }

        public static string CreateOrClause(string condition)
        {
            return CreateConditionalClause("Or", condition);
        }

        public static string CreateFieldRef(string fieldName)
        {
            return "<FieldRef Name='" + fieldName + "' />";
        }

        public static string CreateValueField(string valueType, string value, string fieldName)
        {
            return CreateFieldRef(fieldName) + "<Value Type='" + valueType + "'>" + value + "</Value>";
        }

        public static string CreateBooleanField(string value, string fieldName)
        {
            return CreateValueField("Boolean", value, fieldName);
        }

        public static string CreateIntegerField(string value, string fieldName)
        {
            return CreateValueField("Integer", value, fieldName);
        }

        public static string CreateNumberField(string value, string fieldName)
        {
            return CreateValueField("Number", value, fieldName);
        }

        public static string CreateCriteria(string conditionType, string condition)
        {
            return "<" + conditionType + ">" + condition + "</" + conditionType + ">";
        }

        public static string CreateIsNullCriteria(string condition)
        {
            return CreateCriteria("IsNull", condition);
        }

        public static string CreateIsNotNullCriteria(string condition)
        {
            return CreateCriteria("IsNotNull", condition);
        }

        public static string CreateEqualCriteria(string condition)
        {
            return CreateCriteria("Eq", condition);
        }

        public static string CreateNotEqualCriteria(string condition)
        {
            return CreateCriteria("Neq", condition);
        }

        public static string CreateGreaterThanEqualCriteria(string condition)
        {
            return CreateCriteria("Geq", condition);
        }

        public static string CreateLessThanEqualCriteria(string condition)
        {
            return CreateCriteria("Leq", condition);
        }

        public static string CreateLessThanCriteria(string condition)
        {
            return CreateCriteria("Lt", condition);
        }

        public static string CreateGreaterThanCriteria(string condition)
        {
            return CreateCriteria("Gt", condition);
        }

        public static string CreateBeginsWithCriteria(string condition)
        {
            return CreateCriteria("BeginsWith", condition);
        }

        public static string CreateContainsCriteria(string condition)
        {
            return CreateCriteria("Contains", condition);
        }

        public static string CreateWhereClause(string criteria)
        {
            return "<Where>" + criteria + "</Where>";
        }

        public static string CreateOrderByFieldRef(string fieldName, OrderByType t)
        {
            if (t == OrderByType.Ascending)
                return "<FieldRef Name='" + fieldName + "' Ascending='True' />";
            return "<FieldRef Name='" + fieldName + "' Ascending='False' />";
        }

        //<OrderBy><FieldRef Name='display_x0020_order' Ascending='True' />
        public static string CreateOrderByClause(string criteria)
        {
            return "<OrderBy>" + criteria + "</OrderBy>";
        }

        //<ViewFields><FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='project' /><FieldRef Name='client' /><FieldRef Name='ImageWidth' /><FieldRef Name='ImageHeight' /><FieldRef Name='Description' /><FieldRef Name='show_x0020_in_x0020_viewer' /><FieldRef Name='display_x0020_order' /><FieldRef Name='url' /></ViewFields>

        public static string CreateViewFields(string fieldRefs)
        {
            StringBuilder sb = new StringBuilder("<ViewFields>");
            sb.Append(fieldRefs);
            sb.Append("</ViewFields>");
            return sb.ToString();
        }

        public static string CreateQueryOptions(bool includeMandatoryColumns, bool dateInUtc, int? paging, bool recursive)
        {
            //<QueryOptions></QueryOptions>
            StringBuilder sb = new StringBuilder("<QueryOptions>");
            if (includeMandatoryColumns)
                sb.Append("<IncludeMandatoryColumns>True</IncludeMandatoryColumns>");
            if (dateInUtc)
                sb.Append("<DateInUtc>True</DateInUtc>");
            if (paging != null)
                sb.Append("<Paging>" + paging + "</Paging>");
            if (recursive)
                sb.Append("<ViewAttributes Scope='Recursive' />");
            sb.Append("</QueryOptions>");
            return sb.ToString();
        }

        public static string CreateEmptyQueryOptions()
        {
            return CreateQueryOptions(false, false, null, false);
        }

        public static string CreateQuery(string clauses, string orderby)
        {
            if(String.IsNullOrEmpty(orderby))
                return "<Query>" + clauses + "</Query>";
            return "<Query>" + clauses + orderby + "</Query>";
        }
    }
}
